#include "qs60Telephony.h"
#include "qs60Telephony_p.h"
#include <QDebug>
#include <QTimer>

/*!
    \class QS60Telephony
    \brief The QS60Telephony class can be used for establishing circuit switched calls
    and monitoring the status of the line. Monitoring can be started with the 
    QS60Telephony::startMonitoringLine() function call.
*/    

/*!
    Constructs a QS60Telephony object with the given parent.
    Call error() to get the QS60Telephony::Error value that indicates if an error occurred during construction.
    \sa call(), error()
*/
QS60Telephony::QS60Telephony(QObject* parent) :
    QObject(parent)
  , d(new QS60TelephonyPrivate(this))
  ,cStatus(StatusUnknown)
{
    QTimer::singleShot(0, this, SLOT(startMonitoringLine()));
}

/*!
    Destroys the QS60Telephony object.
*/
QS60Telephony::~QS60Telephony()
{
    delete d;
}

/*!
    \enum QS60Telephony::Error
    This enum defines the possible errors for a QS60Telephony object.
*/
/*! \var QS60Telephony::Error QS60Telephony::NoError
    No error occured.
*/
/*! \var QS60Telephony::Error QS60Telephony::OutOfMemoryError
    Not enough memory.
*/
/*! \var QS60Telephony::Error QS60Telephony::UnknownError
    Unknown error.
*/

/*!
    \enum QS60Telephony::LineStatus
    This enum defines the possible line statuses
*/
/*! \var QS60Telephony::LineStatus QS60Telephony::StatusUnknown
    Status is unknown.
*/
/*! \var QS60Telephony::LineStatus QS60Telephony::StatusIdle
    Status is idle. No active calls.
*/
/*! \var QS60Telephony::LineStatus QS60Telephony::StatusDialling
    Call dialling status.
*/
/*! \var QS60Telephony::LineStatus QS60Telephony::StatusRinging
    Call ringing status.
*/
/*! \var QS60Telephony::LineStatus QS60Telephony::StatusAnswering
    Call answering status.
*/
/*! \var QS60Telephony::LineStatus QS60Telephony::StatusConnecting
    Call connecting status.
*/
/*! \var QS60Telephony::LineStatus QS60Telephony::StatusConnected
    Call connected status.
*/
/*! \var QS60Telephony::LineStatus QS60Telephony::StatusReconnectPending
    Call is undergoing temporary channel loss and it may or may not be  reconnected.
*/
/*! \var QS60Telephony::LineStatus QS60Telephony::StatusDisconnecting
    Call disconnecting status.
*/
/*! \var QS60Telephony::LineStatus QS60Telephony::StatusHold
    Call status hold.
*/
/*! \var QS60Telephony::LineStatus QS60Telephony::StatusTransferring
    Call is transferring.
*/
/*! \var QS60Telephony::LineStatus QS60Telephony::StatusTransferAlerting
    Call in transfer is alerting the remote party.
*/

/*!
    Initiates new call.
    
    Note: If an error occurs while the call is being established, the error() signal is emitted
    
    \param phoneNumber Telephone number to call
    \sa error()
*/
void QS60Telephony::call(const QString& phoneNumber)
{
    d->call(phoneNumber);
}

/*!
    Starts monitoring changes in the line. 
    QS60Telephony::lineStatusChanged(QS60Telephony::LineStatus status, QString number) is emitted
    when there are changes in the line status(e.g. incoming call etc.).

    \return If false is returned, an error has occurred. Call error() to the  
    QS60Telephony::Error value that indicates which error occurred
    \sa error()
*/
bool QS60Telephony::startMonitoringLine()
{
    return d->startMonitoringLine();
}

/*!
    Stops monitoring changes in the line status.
*/
void QS60Telephony::stopMonitoringLine()
{
    d->stopMonitoringLine();
}

QString QS60Telephony::IMEI(){
    return d->IMEI();
}

/*!
    Returns the type of error that occurred if the latest function call failed; otherwise returns NoError.
    \return Error code
*/
QS60Telephony::Error QS60Telephony::error() const
{
    return d->error();
}

/*!
    \fn void QS60Telephony::lineStatusChanged(QS60Telephony::LineStatus status, QString number)
    
    This signal is emitted when line status is changed.

    \param status Current status is retrived as enumeration
    \param number Number if available
    \sa call()
*/

/*!
    \fn void QS60Telephony::error(QS60Telephony::Error error)

    This signal is emitted when an error occurs during a phone call, i.e. after the call() method is called.
    
    \param error Error
*/

void QS60Telephony::answerIncomingCall(){
    qDebug() << "[step #3] Answering incoming call";
    d->answerIncomingCall();
}

void QS60Telephony::terminateCall(){
    if(cStatus == StatusRinging)
        d->rejectCall();
    else
        d->terminateCall();
}

void QS60Telephony::rejectCall(){
    d->rejectCall();
}
#ifdef RECENT_CALLS
void QS60Telephony::recentCalls(){
    d->recentCalls();
}
#endif

void QS60Telephony::toBackground() {
    d->toBackground();
}

void QS60Telephony::showOnTop(){
    QTimer::singleShot(1200, this, SLOT(showOnTopDelayed()));
}

void QS60Telephony::showOnTopDelayed(){
    d->showOnTop();
}

void QS60Telephony::showNormal(){
    d->showNormal();
}

// End of file
